#coding=utf8

import urllib2
import re
import sys
from config import PATH, HEADERS

URL = 'http://music.douban.com/subject_search?search_text=%s&cat=1001'
PAT = re.compile(r'href="http://music.douban.com/musician/(\d+)/"')
headers = HEADERS

def get_musician_id(name):
    url = URL % (name)
    headers['Host'] = 'music.douban.com'
    headers['Referer'] = url
    req = urllib2.Request(url, headers = headers)
    try:
        text = urllib2.urlopen( req ).read()
    except Exception, e:
        print 'Error get_musician_id:', str(e)
        print url
        return None
    res = PAT.search( text )
    if res:
        return res.group(1)
    return None


if __name__ == '__main__':
    mid = get_musician_id( '曲婉婷' )
    print mid
